var subpages = ['list.html', 'category.html', 'search.html', 'user.html'];
var subpage_style = {
	top: '46px',
	bottom: '50px'
}
var anShow = {};
mui.plusReady(function() {
	//隐藏滚动条
	plus.webview.currentWebview().setStyle({
			scrollIndicator: 'none'
	})
		//预加载
	var self = plus.webview.currentWebview(); //获得当前窗口对像
	for (var i = 0; i < 5; i++) {
		var temp = {};
		var sub = plus.webview.create(subpages[i], subpages[i], subpage_style);
		if (i > 0) {
			//将其它先加载
			sub.hide();
		} else {
			//？？？？
			//temp[subpages[i]] = "true";
			//mui.extend(anShow,temp);
		}
		//载入列表页，以插入的方式进来
		self.append(sub);
	}
	//选 项激活
	var activetab = subpages[0];
	var title = document.getElementById("title");
	mui('.mui-bar-tab').on('tap', 'a', function(e) {
		var targetTab = this.getAttribute('href');
		//如果选的是第一个
		if (targetTab == activetab) {
			return;
		}
		//更换标题
		title.innerHTML = this.querySelector('.mui-tab-label').innerHTML;
		//更换目标选项卡
		if (mui.os.ios || anShow[targetTab]) {
			plus.webview.show(targetTab)
		} else {
			//var temp = {};  
			//temp[targetTab] = 'true';
			//mui.extend(anShow,temp);
			plus.webview.show(targetTab);
		}
		//隐藏当前
		plus.webview.hide(activetab);
		//更改当前活跃的选项卡
		activetab = targetTab;
	})

	//返回键处理
	var first = null;
	mui.back = function() {
		//首次按键，提示‘再按一次退出应用’
		if (!first) {
			first = new Date().getTime();
			mui.toast('再按一次退出应用');
			setTimeout(function() {
				first = null;
			}, 1000); 
		} else {
			if (new Date().getTime() - first < 1000) {
				plus.runtime.quit();
			}
		}
	};

	//侧滑菜单延时加载
	var menu = null;
	setTimeout(function() {
		menu = mui.preload({
			id: 'index-menu',
			url: 'index-menu.html',
			styles: {
				left: 0,
				width: '70%',
				zindex: -1
			},
			show: {
				aniShow: 'none'
			}
		});
	}, 200);
	//获得当前窗口对象
	var main = plus.webview.currentWebview();
	//监听窗口关闭事件
	main.addEventListener('maskClick', closeMenu);
	var menuStatus = 0;
	//侧滑菜单
	function openMenu() {
			menuStatus = 1;
			menu.show('none', 0, function() {
				//主窗体开始侧滑并显示遮罩
				main.setStyle({
					mask: 'rgba(0,0,0,0.4)',
					left: '70%',
					transition: {
						duration: 150
					}
				});
				mui.later(function() {
					menu.setStyle({
						mask: "none"
					}); //移除menu的mask
				}, 160);
			});
		}
	//关闭侧滑菜单
	function closeMenu() {
			menuStatus = 0;
			main.setStyle({
				mask: 'none',
				left: '0',
				transition: {
					duration: 200
				}
			})
		}
		//顶部点击展出侧滑菜单
	$("#menu").on('tap', function() {
		openMenu();
	})
})